package com.ruoyi.quartz.task;

import com.ruoyi.quartz.util.DataGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class DataSimulationTask {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Scheduled(fixedRate = 1000) // 每秒执行一次
    public void simulateData() {
        int temperature = DataGenerator.generateTemperature();
        int humidity = DataGenerator.generateHumidity();

        // 将数据插入数据库
        String sql = "INSERT INTO sensor_data (temperature, humidity) VALUES (?, ?)";
        jdbcTemplate.update(sql, temperature, humidity);

//        System.out.println("Data inserted: Temperature=" + temperature + ", Humidity=" + humidity);
    }
}